package com.mmcg.controller;

import com.mmcg.service.GiteeService;
import com.mmcg.service.PortalService;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.domain.Content;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 作者：李嘉浩
 * 时间：2024-09-07-23:44
 * 功能：
 */
@Controller
@RequestMapping
public class PortalController {

    @Autowired
    private PortalService portalService;

    @Autowired
    private GiteeService giteeService;
    //1. 门户首页
    @GetMapping({"/","/index.html"})
    public String index(Model model){

        List<Content> contentList=portalService.findByCategoryId(1L);
        // List<Content> contentList=portalService.findByCategoryId1(1L);
        model.addAttribute("contentList",contentList);
        return "index";  // 转换为物理视图----》  classpath: /templates/index.html
    }

    @GetMapping({"/login.html"})
    public String login(HttpSession session,String returnUrl){
        session.setAttribute("returnUrl",returnUrl);
        return "login";
    }

    /**
     * gitee回调
     * @param code
     * @return
     */
    @GetMapping({"/gitee/callback/success"})
    public String gitee(String code, HttpSession session){
        Map<String,String> map=giteeService.getUserInfo(code);

        session.setAttribute("name",map.get("name"));

        String returnUrl= (String) session.getAttribute("returnUrl");
        if (StringUtils.isNotBlank(returnUrl)){
            return "redirect:"+returnUrl;
        }

        return "login";
    }
}
